package com.antonytrupe.fatcontroller.pc.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

import com.antonytrupe.fatcontroller.EngineListView;
import com.antonytrupe.fatcontroller.Engine;

public class EngineListPanel extends JPanel implements ActionListener,
		com.antonytrupe.fatcontroller.ui.EngineListView {

	private static final String REFRESH_LIST = "refresh_list";
	private JButton refreshButton = new JButton("Refresh List");;
	private JPanel engineList = new JPanel();
	private EngineListView controller;

	private static final long serialVersionUID = -5587264955367504691L;

	public EngineListPanel(ArrayList<Engine> engines, EngineListView controller) {
		this.controller = controller;
		setLayout(new GridLayout(0, 1));
		refreshButton.setActionCommand(REFRESH_LIST);
		refreshButton.addActionListener(this);
		// refreshButton.setBounds(150, 0, 106, 30);

		add(refreshButton);

		// engineList.setBounds(0, 32, 260, 370);
		engineList.setLayout(new GridLayout(0, 1));
		add(engineList);
		displayEngines(engines);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (REFRESH_LIST.equals(e.getActionCommand())) {
			this.refreshEngineList();
		}
	}

	@Override
	public void refreshEngineList() {
		controller.initiateEngineSearch();
	}

	@Override
	public void displayEngine(Engine engine) {
		engineList.add(new EnginePanel(engine, controller));
	}

	@Override
	public void displayEngines(ArrayList<Engine> engines) {
		// ArrayList<Engine> engines = getEngines();
		for (Engine engine : engines) {
			displayEngine(engine);
		}
	}
}